//
//  WBNetworkTools.swift
//  WB123
//
//  Created by xuyunde mac pro on 2017/7/2.
//  Copyright © 2017年 NSObjiect. All rights reserved.
//

import UIKit
import AFNetworking

// 请求方式
enum WBNetworkToolsType:String{
    case get = "get"
    case post = "post"
}

class WBNetworkTools: AFHTTPSessionManager {

    static let share: WBNetworkTools = {
        let tools = WBNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()

    /// 网络请求公共方法封装
    ///
    /// - Parameters:
    ///   - type: 请求方式
    ///   - urlString: 地址
    ///   - parameters: 参数
    ///   - success: 成功的回调
    ///   - failure: 失败的回调
    func request(type: WBNetworkToolsType, urlString: String, parameters: Any?, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        // 判断请求方式
        // get
        if type == .get {
            get(urlString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response)
            }, failure: { (_, error) in
                failure(error)
            })
            
        }else{
            // post
            post(urlString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response)
            }, failure: { (_, error) in
                failure(error)
            })
        }
    }
}

//OAuth需要的网络接口
extension WBNetworkTools{
    // 请求UserAccout
    func oauthLoadUserAccount(code: String, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        // 请求地址
        let urlString = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
            "client_id": HMAPPKEY,
            "client_secret": HMAPPSECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": HMREDIRECT_URI
        ]
        request(type: WBNetworkToolsType.post, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    // 请求个人信息数据
    func oauthLoadUserInfo(userAccountModel: WBUserAccountModel, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        // 请求地址
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 请求参数
        let params = [
            "access_token": userAccountModel.access_token,
            "uid": userAccountModel.uid]
        // 发送网络请求
        request(type: WBNetworkToolsType.get, urlString: urlString, parameters: params, success: success, failure: failure)
    }
}

/*
 since_id	false	int64	若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。
 max_id	false	int64	若指定此参数，则返回ID小于或等于max_id的微博，默认为0。
 */
// MARK: - 请求首页数据接口
extension WBNetworkTools{
    func homeLoadData(since_id:Int64, max_id: Int64, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        //请求地址
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        //请求参数
        let params = ["access_token": WBOAuthViewModel.share.accessToken,
                      "since_id": "\(since_id)",
                        "max_id": "\(max_id)"]
        
        //发送请求
        request(type: .get, urlString: urlString, parameters: params, success: success, failure: failure)
    
    }
}
